package com.umerclass.ui.anmo;

import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Rect;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.appcompat.app.AlertDialog;
import com.umerclass.AppDroid;
import com.umerclass.R;
import com.umerclass.bean.anmo.MyPerformanceBean;
import com.umerclass.bean.anmo.UserOtherBean;
import com.umerclass.frame.model.InfoResult;
import com.umerclass.frame.model.MsgBean;
import com.umerclass.frame.okhttp.OkEntityRequest;
import com.umerclass.frame.okhttp.OkSimpleRequest;
import com.umerclass.frame.ui.activity.BaseActivity;
import com.umerclass.ui.anmo.one.MyTeamActivity;
import com.umerclass.ui.anmo.one.MyYeJiActivity;
import com.umerclass.utils.Constants;
import com.umerclass.utils.EventConstants;
import com.umerclass.utils.screen.ScreenUtil;
import butterknife.BindView;
import butterknife.OnClick;

public class DaiLiActivity extends BaseActivity {

    @BindView(R.id.linear_back)
    LinearLayout linearBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_one)
    TextView tvOne;
    @BindView(R.id.tv_two)
    TextView tvTwo;
    @BindView(R.id.tv1)
    TextView tv1;
    @BindView(R.id.tv2)
    TextView tv2;
    @BindView(R.id.linear1)
    LinearLayout linear1;
    @BindView(R.id.linear2)
    LinearLayout linear2;
    @BindView(R.id.linear3)
    LinearLayout linear3;
    @BindView(R.id.btn_exit)
    Button btnExit;
    private int businessType;//1 一级代理
    private UserOtherBean userOtherBean;

    @Override
    public boolean isImmersionBarEnabled() {
        return false;
    }

    @Override
    public boolean isSupportSwipeBack() {
        return false;
    }

    @Override
    public int getLayoutResId() {
        return R.layout.activity_anmo_dai_li;
    }

    @Override
    public void init() {
        Window window = getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        window.setStatusBarColor(getResources().getColor(R.color.white));
        userOtherBean = AppDroid.getInstance().getUserOtherBean();
        businessType = super.getIntent().getIntExtra("businessType",1);
        if(businessType==1){
            tvTitle.setText("一级代理");
        }else if(businessType==2){
            tvTitle.setText("二级代理");
        }
        tvOne.setText(userOtherBean.getName());
        tvTwo.setText("邀请人: "+ userOtherBean.getInviteBusinessName());
    }

    @Override
    public void loadData() {
        OkEntityRequest<MyPerformanceBean> request = new OkEntityRequest<>(R.id.myPerformance,
                Constants.myPerformance, MyPerformanceBean.class);
        request.addParams("userId", userOtherBean.getUserId());
        request.addParams("operation",0);
        request.addHeadParams("userType",1);
        request.addHeadParams("userToken",userOtherBean.getToken());
        requestOkhttpEntity(request);
    }

    @OnClick({R.id.linear_back,R.id.linear1,R.id.linear2,R.id.linear3,R.id.btn_exit})
    public void OnClick(View view) {
        switch (view.getId()) {
            case R.id.linear_back:
                dialogExit();
                break;
             //我的代理
            case R.id.linear1:
                Intent intent = new Intent(DaiLiActivity.this, MyTeamActivity.class);
                intent.putExtra("businessType",businessType);
                startActivity(intent);
                break;
            //我的业绩
            case R.id.linear2:
                switchTo(DaiLiActivity.this, MyYeJiActivity.class,false);
                break;
            //收益提现
            case R.id.linear3:
                if(dialog == null){
                    createTiXianDialog();
                }else {
                    if(!dialog.isShowing()){
                        password = "";
                        editText.setText("");
                        dialog.show();
                    }
                }
                break;
            case R.id.btn_exit:
                dialogExit();
                break;
            default:
                break;
        }
    }

    @Override
    public void handlerMeg(MsgBean msgBean) {
        super.handlerMeg(msgBean);
        if(msgBean.getFlag() == EventConstants.tixianSuccess){
            loadData();
        }
    }

    @Override
    protected void onSuccess(int what, InfoResult t) {

        switch (what){
            case R.id.myPerformance:
                MyPerformanceBean bean = (MyPerformanceBean) t.getT();
                if(bean!=null){

                    String brokeragePrice = bean.getBrokeragePrice();
                    if(brokeragePrice!=null && !brokeragePrice.equals("")&& !brokeragePrice.equals(" ")){
                        tv1.setText(brokeragePrice);
                    }else {
                        tv1.setText("00");
                    }

                    String price = bean.getBrokeragePrice();
                    if(price!=null && !price.equals("")&& !price.equals(" ")){
                        tv2.setText(price);
                    }else {
                        tv2.setText("00");
                    }

                }
                break;
            case R.id.myPayPassword:
                hideProgress();
                dialog.dismiss();
                Intent intent = new Intent(DaiLiActivity.this,TiXianActivity.class);
                intent.putExtra("password",password);
                startActivity(intent);
                break;
                default:break;
        }
    }

    @Override
    protected void onFail(int what, InfoResult t) {
        switch (what){
            case R.id.myPerformance:
                tv1.setText("0");
                tv2.setText("0");
                break;
            case R.id.myPayPassword:
                break;
            default:break;
        }
    }

    private Dialog dialog;
    private Button btnSure;
    private ImageView imageView;
    private TextView tv_forget_password;
    private EditText editText;
    private String password = "";
    private Dialog createTiXianDialog() {
        if (dialog == null) {
            dialog = new Dialog(this, R.style.ActionSheetDialogStyle);
            //填充对话框的布局
            View inflate = LayoutInflater.from(this).inflate(R.layout.activity_tixian_dialog, null);
            imageView = (ImageView) inflate.findViewById(R.id.image);
            editText = (EditText) inflate.findViewById(R.id.et_password);
            btnSure = (Button) inflate.findViewById(R.id.btn_sure);
            tv_forget_password = (TextView) inflate.findViewById(R.id.tv_forget_password);
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(dialog.isShowing()){
                        dialog.dismiss();
                    }
                }
            });
            tv_forget_password.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(DaiLiActivity.this,FindPasswordActivity.class);
                    intent.putExtra("type",2);
                    startActivity(intent);
                }
            });
            editText.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                }

                @Override
                public void afterTextChanged(Editable s) {

                    if(TextUtils.isEmpty(s.toString())){
                        btnSure.setBackgroundResource(R.drawable.shape_one_normal);
                        password = "";
                    }else {
                        btnSure.setBackgroundResource(R.drawable.shape_one_press);
                        password = s.toString();
                    }
                }
            });
            btnSure.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(!TextUtils.isEmpty(password)){
                        showProgress("正在请求...");
                        OkSimpleRequest okSimpleRequest = new OkSimpleRequest(R.id.myPayPassword,
                                Constants.myPayPassword);
                        okSimpleRequest.addParams("userId", AppDroid.getInstance().getUserOtherBean().getUserId());
                        okSimpleRequest.addParams("payPassword", password);
                        okSimpleRequest.addHeadParams("userType",AppDroid.getInstance().getUserOtherBean().getUserType());
                        okSimpleRequest.addHeadParams("userToken",AppDroid.getInstance().getUserOtherBean().getToken());
                        requestOkhttpSimple(okSimpleRequest);
                    }
                }
            });
            //将布局设置给Dialog
            dialog.setContentView(inflate);
            Window dialogWindow = dialog.getWindow();
            WindowManager.LayoutParams windowparams = dialogWindow.getAttributes();
            //设置Dialog从窗体底部弹出
            dialogWindow.setGravity(Gravity.CENTER);
            Rect rect = new Rect();
            View view1 = dialogWindow.getDecorView();
            view1.getWindowVisibleDisplayFrame(rect);
            windowparams.width = ScreenUtil.getInstance().getScreenWidth();
            dialogWindow.setAttributes(
                    (WindowManager.LayoutParams) windowparams);
            //获得窗体的属性
//            WindowManager.LayoutParams lp = dialogWindow.getAttributes();
//            lp.y = 10;//设置Dialog距离底部的距离
//            //将属性设置给窗体
//            dialogWindow.setAttributes(lp);
            dialog.show();
        }
        return dialog;
    }

    private void dialogExit() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("确认退出？");
        builder.setTitle("提示");
        builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                AppDroid.getInstance().setUserOtherBean(null);
                finish();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int i) {
                dialog.dismiss();
            }
        });
        builder.create().show();
    }
}
